<?php
class App_Track_Search
{
    public static function getTrack($city=NULL,$start=NULL,$end=NULL,
                                    $touristCount=-1,$type = NULL,$age=NULL,
                                    $gender = NULL,$identity=NULL,
                                    $language = NULL,$people = NULL,$chuxing=NULL,
                                    $price=-1,$order=-1,$score=-1,
                                    $limit=30,$offset=0){
        $track = $track_counts = DB::select('m.*','u.*')
            ->from(array('mod_track','m'))
            ->join(array('user_v2','u'))
            ->on('m.uid','=','u.uid')
            ->join(array('user_v2_userinfo','u2i'))
            ->on('u.uid','=','u2i.uid')
            ->where('m.status', '=', 'VALID');

        if(!empty($city) && $city != -1){
            $track = $track_counts = $track->where('m.city','=',$city);
        }

        if(!empty($start)){
            $id = self::get_track_with_start($start);
            if(empty($id)){
                $id = array(0);
            }
            $track = $track_counts = $track->where('m.id','in',$id);
        }

        if(!empty($end)){
            $id = self::get_track_with_end($end);
            if(empty($id)){
                $id = array(0);
            }
            $track = $track_counts = $track->where('m.id','in',$id);
        }

        if($touristCount != -1 && $touristCount != ""){
            $track = $track_counts = $track->where('m.serviceMinPersons','>=',$touristCount);
        }

        if($people != -1 && $people != ""){
            $track = $track_counts = $track->where('m.serviceMaxPersons','<=',$people);
        }

        if($type != NULL && $type == -10){
            $identity = NULL;
            $track = $track_counts = $track->where('m.uid','=',1);
        }

        if(!empty($age) && $age != -1){
//            $key = 'support'.$age.'s';
//            $track = $track_counts = $track->where('m.'.$key,'=',1);
            $track = $track_counts = $track->where('u2i.userAge','=',$age.'后');
        }

        if(!empty($gender)){
            $track = $track_counts = $track->where('u.userGender','=',$gender);
        }

        if(!empty($identity)){
            $track = $track_counts = $track->where('u.identityName','=',$identity);
        }

        if(!empty($language)){
            $key = 'cn';
            if($language == 1){
                $key = 'cn';
            }else if($language == 2){
                $key = 'fr';
            }else if($language == 3){
                $key = 'en';
            }
            $track = $track_counts = $track->where('u.'.$key,'=',1);
        }

        if(!empty($chuxing)){
            $key = "trafficSupportCar";
            if($chuxing == 1){
                $key = "trafficSupportCar";
            }else if($chuxing == 2){
                $key = "trafficSupportBus";
            }
            $track = $track_counts = $track->where('m.'.$key,'=',1);
        }

//        if($score == 2){
//            $price = $order = 1;
//        }else if($price == 2){
//            $order = $score = 1;
//        }else if($order == 2){
//            $price = $score = 1;
//        }

        if($price == 2 ){
            $track = $track_counts = $track->order_by('priceTotal','asc');
        }else if($price == 1 ){
            $track = $track_counts = $track->order_by('priceTotal','desc');
        }

        if($order == 2 ){
            $track = $track_counts = $track->order_by('orders','asc');
        }else if($order == 1 ){
            $track = $track_counts = $track->order_by('orders','desc');
        }


        if($score == 2 ){
            $track = $track_counts = $track->order_by('score','asc');
        }else if($score == 1 ){
            $track = $track_counts = $track->order_by('score','desc');
        }


        $counts = $track_counts->execute()->count();

        $track = $track
            ->limit($limit)
            ->offset($offset);

//        echo $track;
        $tracks = $track->execute()->as_array();
        return array($counts,$tracks);

    }

    public static function getRebuildData($data){
        foreach($data as &$v){
            $v['userAge'] = Arr::get(Util_User::getUserInfo($v['uid']),'userAge');
        }
        return $data;
    }
    
    public static function get_track_with_start($date=NULL){
        $data = DB::select('id','freetimeDays')
            ->from('mod_track')
            ->where('status','=','VALID')
            ->execute()
            ->as_array();

        $id = array();
        $time = strtotime($date);
        foreach($data as $v){
            $days = explode(',',$v['freetimeDays']);
            foreach($days as $day){
                $day_time = strtotime($day);
                if($time < $day_time||$time==$day_time){
                    $id[] = $v['id'];
                    break;
                }
            }
        }

        return $id;
    }

    public static function get_track_with_end($date=NULL){
        $data = DB::select('id','freetimeDays')
            ->from('mod_track')
            ->where('status','=','VALID')
            ->execute()
            ->as_array();

        $id = array();
        $time = strtotime($date);
        foreach($data as $v){
            $days = explode(',',$v['freetimeDays']);
            foreach($days as $day){
                $day_time = strtotime($day);
                if($time > $day_time||$time==$day_time){
                    $id[] = $v['id'];
                    break;
                }
            }
        }

        return $id;
    }

}
